//
//  YZPigGroupDeathDetailRecordCell.m
//  VET
//
//  Created by YZ on 2018/7/24.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZPigGroupDeathDetailRecordCell.h"
#import "YZPigGroupDeathDetailRecordImgCell.h"
#import "YZPigGroupDeathDetailRecordAudioCell.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface YZPigGroupDeathDetailRecordCell ()<UICollectionViewDelegate,UICollectionViewDataSource,UITextViewDelegate>

@property (nonatomic, strong) UILabel *dateLb;

@property (nonatomic, strong) UILabel *typeLb;

@property (nonatomic, strong) UILabel *descLb;

@property (nonatomic, strong , readwrite) UICollectionView *imgColloectView;


@end

@implementation YZPigGroupDeathDetailRecordCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.layer.masksToBounds = YES;
        [self.contentView addSubview:self.dateLb];
        [self.contentView addSubview:self.typeLb];
        [self.contentView addSubview:self.descLb];
        [self.contentView addSubview:self.imgColloectView];
        
        [self makeContraints];
    }
    return self;
}

- (void)makeContraints
{
    [self.dateLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kCurrentDeviceNum(20));
        make.top.mas_equalTo(kCurrentDeviceNum(10));
        make.width.mas_lessThanOrEqualTo(200);
    }];
    
    [self.typeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.dateLb);
        make.left.equalTo(self.dateLb.mas_right).offset(kCurrentDeviceNum(10));
        make.width.mas_lessThanOrEqualTo(200);
    }];
    
    [self.descLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dateLb);
        make.top.equalTo(self.dateLb.mas_bottom).offset(kCurrentDeviceNum(10));
        make.width.mas_lessThanOrEqualTo(200);
    }];
    
    [self.imgColloectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dateLb);
        make.right.equalTo(self.contentView.mas_right).offset(-kCurrentDeviceNum(20));
        make.top.equalTo(self.descLb.mas_bottom).offset(kCurrentDeviceNum(10));
        make.height.mas_equalTo(0);
        make.bottom.equalTo(self.contentView.mas_bottom);
    }];
}

#pragma mark -- UICollectionViewDelegate,UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.model.attachmentList.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    YZPigGroupDeathDetailRecordAttachmentModel *model =  (YZPigGroupDeathDetailRecordAttachmentModel *)self.model.attachmentList[indexPath.row];
    if ([model.mediaType containsString:@"mp4"]) {
        YZPigGroupDeathDetailRecordVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YZPigGroupDeathDetailRecordVideoCell" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }else if([model.mediaType containsString:@"aac"])
    {
        YZPigGroupDeathDetailRecordAudioCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YZPigGroupDeathDetailRecordAudioCell" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }else{
        YZPigGroupDeathDetailRecordImgCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YZPigGroupDeathDetailRecordImgCell" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    YZPigGroupDeathDetailRecordAttachmentModel *model =  (YZPigGroupDeathDetailRecordAttachmentModel *)self.model.attachmentList[indexPath.row];
    if ([model.mediaType containsString:@"mp4"]){
        if (_delegate && [_delegate respondsToSelector:@selector(deathDetailRecordVideoDidSelectedUrl:sourceUrls:sourceView:)]) {
            [_delegate deathDetailRecordVideoDidSelectedUrl:model.attachmentUrl sourceUrls:self.model.attachmentList sourceView:[collectionView cellForItemAtIndexPath:indexPath]];
        }
    }else
    {
        if (_delegate && [_delegate respondsToSelector:@selector(deathDetailRecordImgDidSelectetdAtCell:sourceUrls:sourceView:)]) {
            [_delegate deathDetailRecordImgDidSelectetdAtCell:self sourceUrls:self.model.attachmentList sourceView:[collectionView cellForItemAtIndexPath:indexPath]];
        }
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize size = CGSizeZero;
    if ([self.model.attachmentList[indexPath.row].mediaType containsString:@"aac"]) {
        size = CGSizeMake(kScreenWidth -  kCurrentDeviceNum(20), kCurrentDeviceNum(60));
    }else
    {
        CGFloat widthHeight =  (kScreenWidth-kCurrentDeviceNum(40)-2*kCurrentDeviceNum(5))/3.0;
        size = CGSizeMake(widthHeight, widthHeight);
    }
    return size;
}

#pragma mark -- setters && getters

- (UILabel *)dateLb
{
    if (!_dateLb) {
        _dateLb = [UILabel new];
        _dateLb.textAlignment = NSTextAlignmentLeft;
        _dateLb.font = kCurrentDeviceFont(16);
        _dateLb.textColor = kColorWithHex(0x9B9B9B);
    }
    return _dateLb;
}

- (UILabel *)typeLb
{
    if (!_typeLb) {
        _typeLb = [UILabel new];
        _typeLb.textAlignment = NSTextAlignmentLeft;
        _typeLb.font = self.dateLb.font;
        _typeLb.textColor = self.dateLb.textColor;
    }
    return _typeLb;
}

- (UILabel *)descLb
{
    if (!_descLb) {
        _descLb = [UILabel new];
        _descLb.textAlignment = NSTextAlignmentLeft;
        _descLb.font = kCurrentDeviceFont(16);
    }
    return _descLb;
}

- (UICollectionView *)imgColloectView
{
    if(!_imgColloectView){
        UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc] init];
        fl.minimumLineSpacing = kCurrentDeviceNum(5);
        fl.minimumInteritemSpacing = kCurrentDeviceNum(5);
        fl.sectionInset = UIEdgeInsetsZero;
        _imgColloectView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:fl];
        [_imgColloectView registerClass:[YZPigGroupDeathDetailRecordImgCell class] forCellWithReuseIdentifier:@"YZPigGroupDeathDetailRecordImgCell"];
        [_imgColloectView registerClass:[YZPigGroupDeathDetailRecordVideoCell class] forCellWithReuseIdentifier:@"YZPigGroupDeathDetailRecordVideoCell"];
        [_imgColloectView registerClass:[YZPigGroupDeathDetailRecordAudioCell class] forCellWithReuseIdentifier:@"YZPigGroupDeathDetailRecordAudioCell"];
        _imgColloectView.backgroundColor = kColorWithHex(0xFFFFFF);
        _imgColloectView.dataSource = self;
        _imgColloectView.delegate = self;
        _imgColloectView.scrollEnabled = NO;
    }
    return _imgColloectView;
}

- (void)setModel:(YZPigGroupDeathDetailRecordModel *)model
{
    _model = model;
    self.dateLb.text = model.gmtModified;
    self.typeLb.text = model.trackType.itemValue.length >0?[NSString stringWithFormat:@"(%@)",model.trackType.itemValue]:@"";
    self.descLb.text = model.trackText;
    [self.imgColloectView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo([model.attachmentList.firstObject.mediaType containsString:@"aac"]?kCurrentDeviceNum(60):   model.attachmentList.count/3*kCurrentDeviceNum(5)+ceil(model.attachmentList.count/3.0)*kCurrentDeviceNum((kScreenWidth-40-2*5)/3));
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-kCurrentDeviceNum(10));
    }];
    [self.imgColloectView reloadData];
}

@end
